/*
* Project Porcupine
* Copyright (C) 2012  Team Porcupine
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.towerdive.porcupine.maps;

import com.jme3.scene.Node;

/**
* This class is responsible for generating a dungeon-like map (i.e. classic
* Roguelike maps).  The maps will resemble a tiled system (and this data
* structure is used internally during generation), but ultimately all objects
* will be standalone and unstructured.
*
* @author Martin "quill18" Glaude <quill18@towerdive.com>
*/
public class DungeonMap extends TileMap {


    public DungeonMap(Node mapNode) {
        /**
         * Setup a map with reasonable defaults.
         */    
        this(mapNode, 40, 20, 2.0f, 2.0f);
    }

    public DungeonMap(Node mapNode, int width, int height, float tileWidth, float tileHeight) {
        super(mapNode, width, height, tileWidth, tileHeight);
        for(int x=0; x < width; x++) {
            for(int y=0; y < height; y++) {
                tiles[x][y] = new Tile(x, y, TileType.WALL);
                }
        }
        
        int midX = width/2;
        int midY = height/2;

        MakeRoom(midX-2,midY-2,midX+2,midY+2);
        MakeRoom(midX+4,midY-2,midX+9,midY+2);
        MakeCorridor(midX+3,midY,midX+3,midY);
        
        tiles[midX][midY+2].tileType = TileType.PLAYER_START;
        tiles[midX+7][midY+2].tileType = TileType.MONSTER;

        GenerateMapObjects();
    }

    private void MakeRoom(int x1, int y1, int x2, int y2) {
        for(int x = x1; x <= x2; x++) {
            for(int y=y1; y <= y2; y++) {
                tiles[x][y].tileType = TileType.EMPTY;
            }
        }

        //tiles[(x1+x2)/2][(y1+y2)/2].tileType = TileType.PLAYER_START;
    }

    /**
     * Draws a simple corridor using a naive line algo.
     * @param x1
     * @param y1
     * @param x2
     * @param y2 
     */
    private void MakeCorridor(int x1, int y1, int x2, int y2) {

        int x = x1 < x2 ? x1 : x2;
        int y = y1 < y2 ? y1 : y2;
        int xStop = x1 > x2 ? x1 : x2;
        int yStop = y1 > y2 ? y1 : y2;
        
        // Note that this is guaranteed to execute at least once
        while(x <= xStop && y <= yStop) {
            tiles[x][y].tileType = TileType.EMPTY;
            
            int deltaX = xStop-x;
            int deltaY = yStop-x;
            if (deltaX >= deltaY) {
                x++;
            }
            else {
                y++;
            }
        }


    }

}
